import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/jpush/jpush_service.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/cart_tab_page.dart';
import 'package:flutter_shop/pages/category/category_tab_page/category_tab_page.dart';
import 'package:flutter_shop/pages/common/index_page/provider/index_page_provider.dart';
import 'package:flutter_shop/pages/home/home_tab_page/home_tab_page.dart';
import 'package:flutter_shop/pages/member/member_tab_page/member_tab_page.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/utils/loading.dart';
import 'package:flutter_shop/widget/double_tap_back_exit_app.dart';
import 'package:provider/provider.dart';

///Title:入口 <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-02-15 <br/>
///version 1.0 <br/>
class IndexPage extends StatefulWidget {
  static const String CHANGE_INDEX = "IndexPageChangeIndex";

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends BaseState<IndexPage, BasePresenter> with AmapLocationDisposeMixin {
  PageController _pageController;

  ///图标大小
  static const iconSize = 25.0;

  ///当前索引
  int _currentIndex = 0;

  ///tabs
  final List<BottomNavigationBarItem> tabs = <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Image.asset(R.assetsImgIconTabHomeUncheck, width: iconSize, height: iconSize),
      activeIcon: Image.asset(R.assetsImgIconTabHomeChecked, width: iconSize, height: iconSize),
      title: Text("首页"),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(R.assetsImgIconTabCategoryUncheck, width: iconSize, height: iconSize),
      activeIcon: Image.asset(R.assetsImgIconTabCategoryChecked, width: iconSize, height: iconSize),
      title: Text("分类"),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(R.assetsImgIconTabCartUncheck, width: iconSize, height: iconSize),
      activeIcon: Image.asset(R.assetsImgIconTabCartChecked, width: iconSize, height: iconSize),
      title: Text("购物车"),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(R.assetsImgIconTabMemberUncheck, width: iconSize, height: iconSize),
      activeIcon: Image.asset(R.assetsImgIconTabMemberChecked, width: iconSize, height: iconSize),
      title: Text("会员中心"),
    )
  ];

  ///pages
  final List<Widget> pages = [
    HomeTabPage(), //首页
    CategoryTabPage(), //分类
    CartTabPage(), //购物车
    MemberTabPage(), //会员中心
  ];

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
    //绑定context,为了使receiver能使用Navigator
    JpushService().bindContext(context);
  }

  @override
  void buildComplete() {
    ///请求位置信息
    _requestLocation();
  }

  @override
  bool get useEventBus => true;

  @override
  void onEventBus(EventMessage event) {
    switch (event.tag) {
      case IndexPage.CHANGE_INDEX:
        _pageController.jumpToPage(event.int1);
        break;

      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    //初始化loading
    Loading(context);

    return DoubleTapBackExitApp(
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: tabs,
          currentIndex: _currentIndex,
          unselectedFontSize: 12,
          selectedFontSize: 14,
          onTap: (index) {
            _pageController.jumpToPage(index);
          },
        ),
        body: PageView(
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
          children: pages,
        ),
      ),
    );
  }

  ///请求位置信息
  void _requestLocation() async {
    //单次定位
    Location location = await AmapLocation.fetchLocation(
      mode: LocationAccuracy.High,
      needAddress: true,
    );

    //设置位置信息
    Provider.of<IndexPageProvider>(context, listen: false).setLocation(location);
  }
}
